package org.bigjava.travel.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bigjava.travel.bean.ScenicSpot;
import org.bigjava.travel.dao.ScenicSpotDao;
import org.bigjava.travel.service.ScenicSpotService;
import org.bigjava.travel.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 景点的业务逻辑层实现类
 */

@Service
public class ScenicSpotServiceImpl implements ScenicSpotService {

    @Resource
    ScenicSpotDao scenicSpotDao;

    @Override
    public long count() throws Exception {
        return scenicSpotDao.count();
    }

    @Override
    public long count2() throws Exception {
        return scenicSpotDao.count2();
    }

    @Override
    public ScenicSpot findById(Integer id) throws Exception {
        return scenicSpotDao.findById(id);
    }

    @Override
    public List<ScenicSpot> findList() throws Exception {
        return scenicSpotDao.findList();
    }

    @Override
    public void save(ScenicSpot scenicSpot) throws Exception {
        scenicSpotDao.save(scenicSpot);
    }

    @Override
    public void update(ScenicSpot scenicSpot) throws Exception {
        scenicSpotDao.update(scenicSpot);
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        scenicSpotDao.deleteById(id);
    }

    @Override
    public List<ScenicSpot> findByPage(int currentPage, int pageSize, String query) {
        List<ScenicSpot> list = new ArrayList<ScenicSpot>();
        PageHelper.startPage(currentPage, pageSize);
        if (Tools.notEmpty(query)) {
            list = scenicSpotDao.findListByQuery("%" + query + "%");
        } else {
            list = scenicSpotDao.findList();
        }
        PageInfo<ScenicSpot> pageInfo=new PageInfo<ScenicSpot>(list);
        return pageInfo.getList();
    }

    @Override
    public List<ScenicSpot> findByPage(int currentPage, int pageSize) {
        List<ScenicSpot> list = new ArrayList<ScenicSpot>();
        PageHelper.startPage(currentPage, pageSize);
        list = scenicSpotDao.indexList();
        PageInfo<ScenicSpot> pageInfo=new PageInfo<ScenicSpot>(list);
        return pageInfo.getList();
    }
}
